<template>
  <div>
    <el-form
        :model="verifyForm"
        :rules="rules"
        ref="verifyFormRef"
        label-width="100px"
    >
      <el-form-item label="原密码" prop="oldPassword">
        <el-input
            v-model="verifyForm.oldPassword"
            type="password"
            placeholder="请输入原密码"
            autocomplete="off"
        />
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'

const props = defineProps({
  savedPassword: String
})


const emit = defineEmits(['verified'])

const verifyFormRef = ref(null)
const verifyForm = reactive({
  oldPassword: ''
})
const rules = {
  oldPassword: [
    { required: true, message: '请输入原密码', trigger: 'blur' }
  ]
}

const cancel = () => {
  emit('verified', false)
  verifyForm.oldPassword = ''
}
const submit = () => {
  verifyFormRef.value.validate(valid => {
    if (!valid) return
    if (verifyForm.oldPassword === props.savedPassword) {
      ElMessage.success('验证通过，请输入新密码')
      emit('verified', true)
      verifyForm.oldPassword = ''
    } else {
      ElMessage.error('原密码错误')
    }
  })
}
</script>
